package javaParser;
/****************************updated by fmfmfm on April 1st, 2008*********************************/
/**
 * 1)add ToRdf, send, parserFiels methods.
 * 2)remove content from main method to parserFiles method. We need keep main method clean and clear.
 * 3) modify parserFiles method, add JavaParser variable 
 * There are several issues and need improve later
 * 1) need decide return type for ToRdf method. What type is better? a model or a file?
 * 2) implement send method.
 * 3) anything else? we can put it here later
 /****************************fmfmfm****************************************************************/   
public class Main {
    
	private static final String HOST = "localhost";
	private static final String PROJECT = "default";
	private static final String OUTPUTXML = "output.xml";
	
	
    /**
     * Creates a new instance of Main
     */
    public Main() {
    }
    //parse source files and returns the tree
    public static RdfTree parseFiles(String[] args){
      JavaParser parser ;
      int number_of_files = args.length;
      if (args.length==0) {
    	  System.out.println("usage synopsis: java Main [file1] [file2] . . .");
    	  System.exit(0);
      }
      int i=0;
      try 
      {
         while(i < number_of_files) 
         {
            try
            {
                parser = new JavaParser(new java.io.FileInputStream(args[i]));
/********************************updated by fmfmfm**********************************/               
                 parser.CompilationUnit();                           
            }
            catch (java.io.FileNotFoundException e) 
            {
               System.out.println("File " + args[i] + " not found.");
               return null;
            } catch (ParseException e) {
                  System.out.println(e.getMessage());
                  System.out.println("Java Parser Version 1.1:  Encountered errors during parse.");
            }    
                    
            i++;
         }

      }
 
      catch (ArrayIndexOutOfBoundsException e) 
      {
         System.out.println("You Entered A wrong Number Of Params: Need "+number_of_files+" files but fewer file names were provided");
         System.exit(1);
      }
      System.out.println("Parser finished populating tree");
      return JavaParser.getTree();  
    }
        /**************************Updated by fmfmfm*****************************/
        /* testing structure of RdfTree*/
    /*    try {
            java.io.PrintStream outfile = new java.io.PrintStream(new java.io.FileOutputStream("output.txt"));
            outfile.println(tree.size());
            for(int i = 0; i < tree.size(); i ++) {          
                outfile.println("class: " + tree.get(i).getName());
                for (int j = 0; j < tree.get(i).sizeOfMethods(); j ++){
                    outfile.println("method: " + tree.get(i).getMethod(j).getName());
                    for (int k = 0; k < tree.get(i).getMethod(j).sizeOfCalls(); k ++) {
                        outfile.println("calls: " + tree.get(i).getMethod(j).getCalls(k).getName());
                    }
                }
            }
        }catch(java.io.IOException e) {
        }//fmfmfm
    }
    */
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    	//parse the files and get the tree
    	RdfTree tree = parseFiles(args);
    	//pass the tree and get me the RDF file
    	if (tree!=null) outputRDF(tree);
    	
    }
    
    private static void outputRDF(RdfTree tree) {
    	SourceOntology ontology = new SourceOntology("");
    	if (ontology.Initialize() == false) {
    		System.out.println("Error initializing source ontology");
    		System.exit(0);
    	}
    	new TreeToRdf().ToRdf(tree, ontology);
    	System.out.println("XML file: " + getXMLName());
    	
    	//Abdulla: update 4/9
    	//sending output.xml to remote service, via RMI
    	new RMISender().Send(getXMLName());
    	
    }
    
    public static String getProject() {
    	return PROJECT;
    }
    public static String getHost() {
    	return HOST;
    }
    public static String getXMLName() {
    	return OUTPUTXML;
    }
    
    
}
